import { putLocalStorageForTest } from '@/utils/localStorage';

export const clearValues = (paramObj, urlx) => {

}

export const isJSON = (str) => {
  if (typeof str == 'string') {
    try {
      JSON.parse(str);
      return true;
    } catch (e) {
      console.log(e);
      return false;
    }
  }else{
    try {
      JSON.stringify(str);
      return true;
    } catch (e) {
      return false;
    }
  }
}

export const rendReqPath = (paramObj, urlx) => {
  var name = paramObj["name"];
  var value = paramObj["value"];
  urlx = urlx.replace("{" + name + "}", value);
  return urlx;
}
const rendJsonValues = (nameAll, value, props) => {
  var reqs = nameAll.split("-");
  var ptype = reqs[0];
  if (ptype == undefined) {
    ptype = "";
  }
  var name = reqs[2];
  var key = reqs[3];
  var apiReqParms = props.apiReqParms;
  var apiReqBodyParams = null;
  var apiReqBodyParmsIndex = 0;

  for (var index in apiReqParms) {
    var paraObj = apiReqParms[index];
    if (paraObj == undefined || paraObj == null) {
      continue;
    }
    var paraObjPtype = paraObj.ptype;
    var paraObjName = paraObj.name;
    if (ptype == "header" && paraObjPtype == "header") {
      if (name == paraObjName) {
        props.apiReqParms[index]["value"] = value;
        
        putLocalStorageForTest(props.apiDetailx.operationId, nameAll, value);
      }
    } else if (paraObjPtype == "path") {
      if (name == paraObjName) {
        props.apiReqParms[index]["value"] = value;
        putLocalStorageForTest(props.apiDetailx.operationId, nameAll, value);
      }
    } else if (paraObjPtype == "body") {
      apiReqBodyParams = paraObj;
      apiReqBodyParmsIndex = index;
    } else if (ptype == "query") {
      if (name == paraObjName) {
        props.apiReqParms[index]["value"] = value;
        putLocalStorageForTest(props.apiDetailx.operationId, nameAll, value);
      }
    }
  }
  if (apiReqBodyParams != null && ptype != "header" && ptype != "path") {
    var actureObj = apiReqBodyParams.children;
    innerFunForParams(actureObj, key, value);
    putLocalStorageForTest(props.apiDetailx.operationId, nameAll, value);
  }
}
const getFileParamName = (apiReqParms) => {

  for (var index in apiReqParms) {
    var apiReqPara = apiReqParms[index];
    var ptype = apiReqPara.ptype;
    if ("formData" == ptype) {
      return apiReqPara.name;
    }
  }
}
const rendFileValues = (fileArr, props) => {
  var apiReqParms = props.apiReqParms;
  var formData = apiReqParms.formData;
  if (formData == undefined) {
    formData = new FormData();
  }
  var fileParamName = getFileParamName(apiReqParms);
  // formData.delete(fileParamName);
  // formData.append(fileParamName, file);
  formData.delete(fileParamName);
  for (var i = 0; i < fileArr.length; i++) {
    var file = fileArr[i];
    formData.append(fileParamName, file);
  }
  apiReqParms.formData = formData;
}
export const renderReqValues = (nameAll, value, props) => {
  if (nameAll == undefined || nameAll == '') {
    rendFileValues(value, props);
  } else {
    rendJsonValues(nameAll, value, props);
  }

  // apiReqParms[apiReqBodyParmsIndex] = apiReqBodyParams;
}
export const innerFunForParams = (actureObj, key, value) => {
  if (actureObj instanceof Array) {

    for (var chItemIndex in actureObj) {
      var chItem = actureObj[chItemIndex];
      innerFunForParams(chItem, key, value);
    }
  } else if (actureObj instanceof Object) {
    var objChItem = actureObj.children;
    if (objChItem != undefined) {
      for (var chItemIndex in objChItem) {
        var chItem = objChItem[chItemIndex];
        innerFunForParams(chItem, key, value);
      }
    } else {
      var keyNow = actureObj["key"];
      if (key == keyNow) {
        actureObj["value"] = value;
      }
    }
  }
}
export const getReqBodyValues = (apiReqParms) => {
  var bodyObj = {};
  for (var index in apiReqParms) {
    var paramObj = apiReqParms[index];
    var ptype = paramObj["ptype"];
    if (ptype == "body") {
      getReqValues(bodyObj, paramObj, true);
    }
  }
  return bodyObj;
}
export const getReqValues = (bodyObj, paramObj, nullAble) => {
  var rootPara = paramObj.children;
  if (rootPara != undefined) {
    for (var index in rootPara) {
      var pItem = rootPara[index];
      var type = pItem["type"];
      var name = pItem["name"];
      if (type == "object") {
        bodyObj[name] = {};
        getReqValues(bodyObj[name], pItem);
      } else if (type.indexOf("List") > -1) {//需要改进，基础类型的泛型不需要作为子节点
        if (type.indexOf("List<string>") > -1 || type.indexOf("List<int>") > -1) {
          var value = pItem["value"];
          if (value != undefined) {
            bodyObj[name] = {};
            if (!(value instanceof Array)) {
              try {
                value = eval(value);
              } catch{

              }
            }
            bodyObj[name] = value;
          } else {
            if (nullAble) {
              bodyObj[name] = [];
            }
          }
        } else {
          bodyObj[name] = [];
          bodyObj[name][0] = {};
          getReqValues(bodyObj[name][0], pItem);
        }
      } else {
        var value = pItem["value"];
        if (value != undefined) {
          bodyObj[name] = {};
          bodyObj[name] = value;
        } else {
          if (nullAble) {
            bodyObj[name] = '';
          }
        }
      }
    }
  } else {
    var name = paramObj["name"];
    var value = paramObj["value"];
    if (value != undefined) {
      bodyObj[name] = {};
      bodyObj[name] = value;
    } else {
      if (nullAble) {
        bodyObj[name] = '';
      }
    }
  }
}